package nf.modular;

import java.io.File;

public abstract class NFModule
{
	protected ModuleHost host = null;
	private String lastError = null;
	protected String scratchDir = null;
	private boolean initDone = false;
	private boolean initFailed = false;
	
	protected NFWiring wiring = null;
	
	public NFModule(ModuleHost host, String scratchDir)
	{
		this.host = host;
		this.scratchDir = scratchDir;
	}
	
	public final void setWiring(NFWiring wiring)
	{
		this.wiring = wiring;
	}
	
	protected abstract boolean doInit();
	
	public boolean init()
	{
		if(initFailed) return false;
		if(initDone) return true;
		
		initDone = doInit();
		if(initDone)
			return true;
		else
			return !(initFailed = true);
	}
	
	public static class NFModuleSignature
	{
		public String name = "";
		public int versionInt = 0;
		public String versionString = "";
		public NFModuleType type = NFModuleType.Unknown;
		
		public NFModuleSignature(String name, int versionInt, String versionString, NFModuleType type)
		{
			this.name = name;
			this.versionInt = versionInt;
			this.versionString = versionString;
			this.type = type;
		}
	}
	
	abstract public NFModuleType getModuleType();
	
	abstract public String getModuleName();
	abstract public int getVersionInt();
	abstract public String getVersionString();
	abstract public String getAuthor();
	abstract public String getHTMLDescription();
	
	abstract public boolean canSupportParamInt(String name, int value);
	abstract public boolean canSupportParamDouble(String name, double value);
	abstract public boolean canSupportParamString(String name, String value);
	
	abstract public boolean setParamInt(String name, int value);
	abstract public boolean setParamDouble(String name, double value);
	abstract public boolean setParamString(String name, String value);
	
	abstract public boolean checkRequirements();
	
	abstract public boolean checkWiring(NFWiring wiring);
	
	abstract public boolean setupParameters();
	
	abstract public boolean isUsingDataFiles();
	abstract public boolean useDataFile(File dataFile);
	
	public boolean checkCompliance(NFModuleSignature sig)
	{
		if(sig.name.compareTo(getModuleName()) != 0) return false;
		if(sig.versionInt > getVersionInt()) return false;
		return true;
	}
	
	public boolean setCompliance(NFModuleSignature sig)
	{
		return checkCompliance(sig);
	}
	
	public final String getLastError()
	{
		return lastError;
	}
	
	protected final void setError(String e)
	{
		lastError = e;
	}
	
	protected final void clearError()
	{
		lastError = null;
	}
	
	public String toString()
	{
		return getModuleName() + " v" + getVersionString() + " (" + getVersionInt() + ")";
	}
}
